
// CircuitObjDoc.cpp : implementation of the CCircuitObjDoc class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "CircuitObj.h"
#endif

#include "CircuitObjDoc.h"
#include "SrvrItem.h"

#include <propkey.h>
#include "MainFrm.h"
#include "CirCuitObjView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
extern CCircuitObjApp theApp;
// CCircuitObjDoc

IMPLEMENT_DYNCREATE(CCircuitObjDoc, COleServerDoc)

BEGIN_MESSAGE_MAP(CCircuitObjDoc, COleServerDoc)

END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CCircuitObjDoc, COleServerDoc)
END_DISPATCH_MAP()

// Note: we add support for IID_ICircuitObj to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .IDL file.

// {FF327A42-10DD-4758-91FD-AFACAF623249}
static const IID IID_ICircuitObj =
{ 0xFF327A42, 0x10DD, 0x4758, { 0x91, 0xFD, 0xAF, 0xAC, 0xAF, 0x62, 0x32, 0x49 } };

BEGIN_INTERFACE_MAP(CCircuitObjDoc, COleServerDoc)
	INTERFACE_PART(CCircuitObjDoc, IID_ICircuitObj, Dispatch)
END_INTERFACE_MAP()


// CCircuitObjDoc construction/destruction

CCircuitObjDoc::CCircuitObjDoc()
{
	// Use OLE compound files
	EnableCompoundFile();

	// TODO: add one-time construction code here

	EnableAutomation();

	AfxOleLockApp();
}

CCircuitObjDoc::~CCircuitObjDoc()
{
	AfxOleUnlockApp();
}

BOOL CCircuitObjDoc::OnNewDocument()
{
	if (!COleServerDoc::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}


// CCircuitObjDoc server implementation

COleServerItem* CCircuitObjDoc::OnGetEmbeddedItem()
{
	// OnGetEmbeddedItem is called by the framework to get the COleServerItem
	//  that is associated with the document.  It is only called when necessary.

	CCircuitObjSrvrItem* pItem = new CCircuitObjSrvrItem(this);
	ASSERT_VALID(pItem);
	return pItem;
}


CCircuitObjView *CCircuitObjDoc::GetActiveView()
{
	POSITION pos = GetFirstViewPosition();
	while (pos != NULL)
	{
		CCircuitObjView* pView = (CCircuitObjView*)GetNextView(pos);
		return pView;
	}

	return NULL;
}

// CCircuitObjDoc serialization

void CCircuitObjDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		POSITION pos = GetFirstViewPosition();
		while (pos != NULL)
		{
			CCircuitObjView* pView = (CCircuitObjView*)GetNextView(pos);
			if( pView)
				pView->GetHoopsModel()->Serialize(ar);
		}

	}
	else
	{
		POSITION pos = GetFirstViewPosition();
		while (pos != NULL)
		{
			CCircuitObjView* pView = (CCircuitObjView*)GetNextView(pos);
			if( pView)
			{
				HCircuitModel *pModel = pView->GetHoopsModel();
				if( pModel == NULL )
				{
					pModel = new HCircuitModel();
					pModel->Init();
					pView->SetHoopsModel(pModel);
				}
				pModel->Serialize(ar);
				HBaseView *pHoopsView = pView->GetHoopsView();
				if( pHoopsView == NULL )
				{
					pView->Init();
					pHoopsView = pView->GetHoopsView();
				}

				pView->RenderCircuit(pModel->GetCurCirCuitDisObj());

			}
		}

	}
}

#ifdef SHARED_HANDLERS

// Support for thumbnails
void CCircuitObjDoc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds)
{
	// Modify this code to draw the document's data
	dc.FillSolidRect(lprcBounds, RGB(255, 255, 255));

	CString strText = _T("TODO: implement thumbnail drawing here");
	LOGFONT lf;

	CFont* pDefaultGUIFont = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
	pDefaultGUIFont->GetLogFont(&lf);
	lf.lfHeight = 36;

	CFont fontDraw;
	fontDraw.CreateFontIndirect(&lf);

	CFont* pOldFont = dc.SelectObject(&fontDraw);
	dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK);
	dc.SelectObject(pOldFont);
}

// Support for Search Handlers
void CCircuitObjDoc::InitializeSearchContent()
{
	CString strSearchContent;
	// Set search contents from document's data. 
	// The content parts should be separated by ";"

	// For example:  strSearchContent = _T("point;rectangle;circle;ole object;");
	SetSearchContent(strSearchContent);
}

void CCircuitObjDoc::SetSearchContent(const CString& value)
{
	if (value.IsEmpty())
	{
		RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid);
	}
	else
	{
		CMFCFilterChunkValueImpl *pChunk = NULL;
		ATLTRY(pChunk = new CMFCFilterChunkValueImpl);
		if (pChunk != NULL)
		{
			pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT);
			SetChunkValue(pChunk);
		}
	}
}

#endif // SHARED_HANDLERS

// CCircuitObjDoc diagnostics

CSize CCircuitObjDoc::GetCircuitSize()
{
	CCircuitObjView *pView = GetActiveView();
	if( pView )
	{
		HCircuitModel * pModel = pView->GetHoopsModel();

		CICircuitDisObj*pCircuitDisObj = pModel->GetCurCirCuitDisObj();

		if( pCircuitDisObj )
		{
			int cx = int( pCircuitDisObj->GetBoxMaxX()-pCircuitDisObj->GetBoxMinX() );
			int cy = int( pCircuitDisObj->GetBoxMaxY()-pCircuitDisObj->GetBoxMinY() );
			return CSize(cx*2,cy*2);
		}
	}
	return CSize(3000,3000);
}


void CCircuitObjDoc::OnCloseDocument()
{ 
	COleServerDoc::OnCloseDocument();
}

#ifdef _DEBUG
void CCircuitObjDoc::AssertValid() const
{
	COleServerDoc::AssertValid();
}

void CCircuitObjDoc::Dump(CDumpContext& dc) const
{
	COleServerDoc::Dump(dc);
}



#endif //_DEBUG


// CCircuitObjDoc commands
